<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * Create ORE aggregation
 * 
 * Create an ORE aggregation.
 */
class escape_repositoryui_rpc_CreateOreAggregation extends escape_repositoryui_RpcHandler
{
	public function handleRequest()
	{
		$title = $_REQUEST['title'];
		$creator = $_REQUEST['creator'];
		
		// only administrators and creators are allowed to create aggregations
		$user = $this->repositoryUi->user;
		if($user === null || (!$user->hasSystemRole(escape_repository_User::$role_administrator) && !$user->hasSystemRole(escape_repository_User::$role_creator)))
		{
			throw new escape_repositoryui_exception_Rpc("Not authorized");
		}

		// create the aggregation
		$aggregation = $this->repository->createOreAggregation();
		if($aggregation === null)
		{
			throw new escape_repositoryui_exception_Exception('Failed to create a new aggregation');
		}
		
		// add basic metadata
		$rdfResource = $aggregation->getProperties();
		$rdfResource->addStatement('http://purl.org/dc/terms/title', $title);
		$rdfResource->addStatement('http://purl.org/dc/terms/creator', $creator);
		$rdfResource->removeStatement('http://purl.utwente.nl/ns/escape-system.owl#createdBy');
		$rdfResource->addStatement('http://purl.utwente.nl/ns/escape-system.owl#createdBy', $user->getId());
		$rdfResource->removeStatement('http://purl.utwente.nl/ns/escape-system.owl#lastModifiedBy');
		$rdfResource->addStatement('http://purl.utwente.nl/ns/escape-system.owl#lastModifiedBy', $user->getId());
		$aggregation->setProperties($rdfResource);
		
		// add the creator of the aggregation to the aggregation owners
		$aggregation->addOwner($user->getUri());
		
		// return the URI of the new aggregation in JSON format
		print json_encode($aggregation->getUri());
	}
}
